package com.reny.ll.git.base_logic.ext

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.orhanobut.logger.Logger
import com.reny.ll.git.base_logic.utils.ClickUtils
import com.reny.ll.git.core.App
import com.reny.ll.git.core.act
import es.dmoral.toasty.Toasty
import org.greenrobot.eventbus.EventBus


fun Any.router(): ARouter{
    return ARouter.getInstance()
}

fun Any.postEvent(){
    EventBus.getDefault().post(this)
    //notify flutter
    //Faraday.postNotification(this.javaClass.simpleName, gson.toJson(this))
}

fun Any.loge(str:String, vararg other:Any?){
    if(App.isDebug) {
        Logger.e(str, other)
    }
}

fun Any.singleClick(clickFunc: () -> Unit){
    if(!ClickUtils.isFastClick()){
        clickFunc()
    }
}

//防止短时间多次点击
fun View.onClick(clickFunc: () -> Unit){
    setOnClickListener {
        singleClick{
            clickFunc()
        }
    }
}

//多个view点击事件相同 方便使用
fun Any.onClick(vararg views: View, clickFunc: () -> Unit){
    views.forEach {
        it.setOnClickListener {
            singleClick{
                clickFunc()
            }
        }
    }
}


fun Any.isEmpty(value: Any?):Boolean{
    if(value == null){
        return true
    }else{
        if(value is String){
            return value.isEmpty() || "null" == value
        }
        if(value is Collection<*>){
            return value.isEmpty()
        }
        if(value is Map<*, *>){
            return value.isEmpty()
        }
        return false
    }
}


object ToastExt{
    var toast: Toast? = null
}
fun innerToast(text:Any, context: Context, duration:Int){
    ToastExt.toast?.cancel()
    if(text is String) {
        ToastExt.toast = Toasty.normal(context, text, duration)
        ToastExt.toast!!.setGravity(Gravity.CENTER, 0, 0)
        ToastExt.toast!!.show()
    }
    if(text is Int){
        ToastExt.toast = Toasty.normal(context, text, duration)
        ToastExt.toast!!.setGravity(Gravity.CENTER, 0, 0)
        ToastExt.toast!!.show()
    }
}

fun Any.toast(text:Any?, context: Context? = null, duration:Int = Toast.LENGTH_LONG){
    if(isEmpty(text)){
        loge("${this.javaClass.simpleName} >> showToast(null)")
        return
    }
    if(text is String || text is Int) {
        val act: Activity? = when (this) {
            is Activity -> {
                this
            }
            is Fragment -> {
                this.activity
            }
            else -> {
                act()
            }
        }
        if (act != null) {
            if(!act.isDestroyed) {
                act.runOnUiThread {
                    innerToast(text, context ?: App.instance, duration)
                }
            }
        } else {
            try {
                innerToast(text, App.instance, duration)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }else{
        throw RuntimeException("showToast text参数类型传入错误")
    }
}